using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Principal;

namespace EAT.ConfigurationUtility
{
    [Guid("38AC40B0-2DAF-4ba6-8A1A-978ADCFC6FD2")]
    public class ConfigurationUtilityReceiver : SPFeatureReceiver
    {
        String OwnerName = "EATConfiguration";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication spWebApp = (SPWebApplication) properties.Feature.Parent;

            createModification(spWebApp, "appSettings", "/configuration", "<appSettings />",
                                SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode);
            // add modifications

            spWebApp.WebService.ApplyWebConfigModifications();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        private void createModification(
                                        SPWebApplication webApp,
                                        string Name, 
                                        string XPath,
                                        string Value,
                                        SPWebConfigModification.SPWebConfigModificationType modType
                                        )
        {
            SPWebConfigModification modification = new SPWebConfigModification();
            modification.Name = Name;
            modification.Path = XPath;
            modification.Owner = OwnerName;
            modification.Sequence = 0;
            modification.Type = modType;
            modification.Value = Value;
            webApp.WebConfigModifications.Add(modification);
            return;
        }
    }
}